﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Net.WebSockets;
using System.Web;
using System.Web.WebPages;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using Newtonsoft.Json;
using NPOI.XSSF.Streaming;
using NPOI.XSSF.UserModel;
using SignatureWebApi.Models;

namespace SignatureWebApi.Models
{

    /// <summary>
    /// 签单数据更新模型
    /// </summary>
    public class SignatureUpdateModel
    {
        /// <summary>
        /// 表单号
        /// </summary>
        [Required]
        [BsonElement("No")]
        [Display(Name = "表单号")]
        public string No { get; set; }
        /// <summary>
        /// 桥数据
        /// </summary>
        [Required]
        [DataValidation(true)]
        [BsonElement("bridgeData")]
        [Display(Name = "桥数据")]
        public BridgeModel BridgeData{ get; set; }
        /// <summary>
        /// 空调数据
        /// </summary>
        [Required]
        [DataValidation(true)]
        [BsonElement("pcaData")]
        [Display(Name = "空调数据")]
        public PcaModel PcaData { get; set; }

        /// <summary>
        /// 电源数据
        /// </summary>
        [Required]
        [DataValidation(true)]
        [BsonElement("adapterData")]
        [Display(Name = "电源数据")]
        public AdapterModel AdapterData { get; set; }

        /// <summary>
        /// 客梯车数据(次数)
        /// </summary>
        [BsonElement("psData")]
        [Display(Name = "客梯车数据")]
        public int PsData { get; set; }
        /// <summary>
        /// 乘客用车(次数)
        /// </summary>
        [BsonElement("pbData")]
        [Display(Name = "乘客用车")]
        public int PbData { get; set; }
        /// <summary>
        /// 机组用车(次数)
        /// </summary>
        [BsonElement("cbData")]
        [Display(Name = "机组用车")]
        public int CbData { get; set; }
        /// <summary>
        /// 引导车使用次数(次数)
        /// </summary>
        [BsonElement("gData")]
        [Display(Name = "引导车")]
        public int/*PassengerStepData[]*/ GData { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        [BsonElement("Status")]
        [Display(Name = "状态")]
        [EnumCheck(typeof(SignatureStatus))]
        public SignatureStatus Status { get; set; }
        /// <summary>
        /// 使用情况
        /// </summary>
        //[Required]
        [BsonElement("Usage")]
        [Display(Name = "使用情况")]
        public string Usage { get; set; }
        /// <summary>
        /// 签名
        /// </summary>
        [Display(Name = "签名")]
        [BsonElement("Pic")]
        public byte[] Pic { get; set; }
        /// <summary>
        /// 工作人员签名
        /// </summary>
        [Display(Name = "工作人员签名")]
        [BsonElement("SPic")]
        public byte[] StaffSignPic { get; set; }

        /// <summary>
        /// 航班号
        /// </summary>
        [BsonElement("Flt")]
        [Display(Name = "航班号")]
        public string Flt { get; set; }

        /// <summary>
        /// 机号
        /// </summary>
        [BsonElement("Pnum")]
        [Display(Name = "机号")]
        public string Pnum { get; set; }
        /// <summary>
        /// 机型
        /// </summary>
        [BsonElement("PlaneType")]
        [Display(Name = "机型")]
        public string PlaneType { get; set; }
        /// <summary>
        /// 机位
        /// </summary>
        [BsonElement("Stand")]
        [Display(Name = "机位")]
        public string Stand { get; set; }
        /// <summary>
        /// 航空公司
        /// </summary>
        [BsonElement("Airline")]
        [Display(Name = "航空公司")]
        public string Airline { get; set; }
        /// <summary>
        /// 航班类型
        /// </summary>
        [BsonElement("FType")]
        [Display(Name = "航班类型")]
        public FlightType FType { get; set; }
        /// <summary>
        /// 最后一次更新状态
        /// </summary>
        [EnumCheck(typeof(SignatureUpdateType))]
        [BsonElement("LastUpdateType")]
        public SignatureUpdateType LastUpdateType { get; set; }
    }
    /// <summary>
    /// 离线签单数据更新模型
    /// </summary>
    public class OfflineSignatureUpdateModel
    {
        /// <summary>
        /// 表单号
        /// </summary>
        [Required]
        [Display(Name = "表单号")]
        public string No { get; set; }
        /// <summary>
        /// 航班号
        /// </summary>
        [Required]
        [Display(Name = "航班号")]
        public string Flt { get; set; }
        /// <summary>
        /// 机位
        /// </summary>
        //[Required]
        [Display(Name = "机位")]
        public string Stand { get; set; }
        /// <summary>
        /// 航空公司
        /// </summary>
        [Required]
        [Display(Name = "航空公司")]
        public string Airline { get; set; }
        /// <summary>
        /// 机号
        /// </summary>
        //[Required]
        [Display(Name = "机号")]
        public string Pnum { get; set; }
        /// <summary>
        /// 机型
        /// </summary>
        //[Required]
        [Display(Name = "机型")]
        public string PlaneType { get; set; }
        /// <summary>
        /// 航班类型
        /// </summary>
        [Required]
        [EnumCheck(typeof(FlightType))]
        [Display(Name = "航班类型")]
        public FlightType FType { get; set; }
        /// <summary>
        /// 桥数据
        /// </summary>
        [Required]
        [DataValidation]
        [Display(Name = "桥数据")]
        public BridgeModel bridgeData { get; set; }
        /// <summary>
        /// 空调数据
        /// </summary>
        [Required]
        [DataValidation]
        [Display(Name = "空调数据")]
        public PcaModel pcaData { get; set; }
        /// <summary>
        /// 电源数据
        /// </summary>
        [Required]
        [DataValidation]
        [Display(Name = "电源数据")]
        public AdapterModel adapterData { get; set; }
        /// <summary>
        /// 客梯车数据(次数)
        /// </summary>
        [Display(Name = "客梯车数据")]
        public int/*PassengerStepData[]*/ psData { get; set; }
        /// <summary>
        /// 机组用车(次数)
        /// </summary>
        [Display(Name = "机组用车")]
        public int/*PassengerStepData[]*/ cbData { get; set; }
        /// <summary>
        /// 乘客用车(次数)
        /// </summary>
        [Display(Name = "乘客用车")]
        public int/*PassengerStepData[]*/ pbData { get; set; }
        [Display(Name = "引导车")]
        public int/*PassengerStepData[]*/ gData { get; set; }
        /// <summary>
        /// 使用情况
        /// </summary>
        [Display(Name = "使用情况")]
        public string Usage { get; set; }
        /// <summary>
        /// 签名时间
        /// </summary>
        [Required]
        [Display(Name = "签名时间")]
        public DateTime Time { get; set; }
        /// <summary>
        /// 机组签名
        /// </summary>
        [Display(Name = "机组签名")]
        [BsonElement("Pic")]
        [Required]
        public byte[] Pic { get; set; }
        /// <summary>
        /// 工作人员签名
        /// </summary>
        [Display(Name = "工作人员签名")]
        [BsonElement("SPic")]
        [Required]
        public byte[] StaffSignPic { get; set; }
        ///// <summary>
        ///// 签单状态
        ///// </summary>
        //[Required]
        //[Display(Name = "签单状态")]
        //[EnumCheck(typeof(SignatureStatus))]
        //public SignatureStatus Status { get; set; }
        /// <summary>
        /// 签单创建用户Id
        /// </summary>
        [Required]
        [BsonElement("User")]
        [Display(Name = "签单操作用户")]
        public string UserId { get; set; }
        /// <summary>
        /// 签单类型
        /// </summary>
        [BsonElement("SType")]
        [Display(Name = "签单类型")]
        public SignatureType SignatureType { get; set; }
    }
    /// <summary>
    /// 关闭签单请求模型
    /// </summary>
    public class CloseSignatureRequestModel
    {
        /// <summary>
        /// 表单号
        /// </summary>
        [Required]
        [Display(Name = "表单号")]
        public string No { get; set; }
    }
    /// <summary>
    /// 签单数据请求模型
    /// </summary>
    public class SignatureRequestModel
    {
        /// <summary>
        /// 区域
        /// </summary>
        [Display(Name = "区域")]
        public StandAreaEnum? Area { get; set; }
        /// <summary>
        /// 机位
        /// </summary>
        [Display(Name = "机位")]
        public string Stand { get; set; }
        /// <summary>
        /// 航班号
        /// </summary>
        [Display(Name = "航班号")]
        public string Flt { get; set; }
        /// <summary>
        /// 航空公司
        /// </summary>
        [Display(Name = "航空公司")]
        public string Airline { get; set; }


        /// <summary>
        /// 签单修改人
        /// </summary>
        [Display(Name = "签单修改人")]
        public string User { get; set; }
        /// <summary>
        /// 开始时间
        /// 若跳页
        /// 如上一页检索为false:时间为当前表格最后一行(数据按时间从小到大排列)的Time
        /// 如上一页检索为true: 时间为当前表格第一行(数据按时间从小到大排列)的Time
        /// </summary>
        [Display(Name = "开始时间")]
        public DateTime Time { get; set; }
        /// <summary>
        /// 是否上一页 true为上一页 false为下一页(即按时间向前或向后检索)
        /// </summary>
        [Display(Name = "上一页选项")]
        public bool Prev { get; set; }
        /// <summary>
        /// 跳页页数
        /// 如上一页检索为false:当前为15页 想要跳转到20页 则skip为(20-15-1=4)
        /// 如上一页检索为true:当前为15页 想要跳转到10页 则skip为(15-10-1=4)
        /// </summary>
        [Display(Name = "跳页页数")]
        public int Skip { get; set; }
        /// <summary>
        /// 跳页时的排除的数据Id(数组)
        /// </summary>
        [Display(Name = "排除数据Id")]
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string[] ObjectId { get; set; }

        /// <summary>
        /// 签单状态
        /// </summary>
        [EnumCheck(typeof(SignatureStatus))]
        public SignatureStatus? Status { get; set; }
    }
    /// <summary>
    /// 导出请求模型
    /// </summary>
    public class SignatureExportRequestModel
    {
        /// <summary>
        /// 区域
        /// </summary>
        public StandAreaEnum? Area { get; set; }
        /// <summary>
        /// 机位
        /// </summary>
        public string Stand { get; set; }
        /// <summary>
        /// 航班号
        /// </summary>
        public string Flt { get; set; }
        /// <summary>
        /// 航空公司
        /// </summary>
        public string Airline { get; set; }
        /// <summary>
        /// 签单状态
        /// </summary>
        [EnumCheck(typeof(SignatureStatus))]
        public SignatureStatus? Status { get; set; }
        /// <summary>
        /// 导出模式
        /// </summary>
        [EnumCheck(typeof(ExportMode))]
        [Required]
        public ExportMode Mode { get; set; }
        /// <summary>
        /// 按月导出模式中的月份
        /// </summary>
        public DateTime Month { get; set; }
        /// <summary>
        /// 按时间模式导出中的时间,index0为起始时间,index1为结束时间
        /// </summary>
        public DateTime[] Range { get; set; }
        /// <summary>
        /// 航班类型
        /// </summary>
        [EnumCheck(typeof(FlightType))]
        public FlightType? FType { get; set; }
        /// <summary>
        /// 签单类型
        /// </summary>
        [EnumCheck(typeof(SignatureType))]
        public SignatureType? SType { get; set; }
        /// <summary>
        /// 机号
        /// </summary>
        public string Pnum { get; set; }
        /// <summary>
        /// 机型
        /// </summary>
        public string PlaneType { get; set; }
        /// <summary>
        /// 签单用户
        /// </summary>
        public string UserName { get; set; }
       
    }

    public class SignatureExportSpecialRequestModel
    {
        /// <summary>
        /// 导出模式
        /// </summary>
        [EnumCheck(typeof(ExportMode))]
        [Required]
        public ExportMode Mode { get; set; }
        /// <summary>
        /// 按月导出模式中的月份
        /// </summary>
        public DateTime Month { get; set; }
        /// <summary>
        /// 按时间模式导出中的时间,index0为起始时间,index1为结束时间
        /// </summary>
        public DateTime[] Range { get; set; }
    }
    /// <summary>
    /// 工作人员在现场通过机位获取签单的请求模型
    /// </summary>
    public class StaffSignatureRequsetModel
    {
        /// <summary>
        /// 机位
        /// </summary>
        [Display(Name = "机位")]
        public string Stand { get; set; }

        /// <summary>
        /// 区域(枚举)
        /// </summary>
        [Display(Name = "区域")]
        [EnumCheck(typeof(StandAreaEnum))]
        public StandAreaEnum? Area { get; set; }

        /// <summary>
        /// 航班号
        /// </summary>
        public string Flt { get; set; }
    }
    /// <summary>
    /// 签单数据返回模型
    /// </summary>
    [BsonIgnoreExtraElements]
    public class SignatureReturnModel
    {
        /// <summary>
        /// 唯一ID
        /// </summary>
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        [BsonElement("_id")]
        public string Id { get; set; }
        /// <summary>
        /// 单号
        /// </summary>
        [Display(Name = "签单号")]
        [BsonElement("No")]
        public string No { get; set; }
        /// <summary>
        /// 航班号
        /// </summary>
        [Display(Name = "航班号")]
        [BsonElement("Flt")]
        public string Flt { get; set; }
        /// <summary>
        /// 机尾号
        /// </summary>
        [Display(Name = "机尾号")]
        [BsonElement("Pnum")]
        public string Pnum { get; set; }
        /// <summary>
        /// 机型
        /// </summary>
        [Display(Name = "机型")]
        [BsonElement("PlaneType")]
        public string PlaneType { get; set; }

        /// <summary>
        /// 机位
        /// </summary>
        [Display(Name = "机位")]
        [BsonElement("Stand")]
        public string Stand { get; set; }
        /// <summary>
        /// 航空公司
        /// </summary>
        [Display(Name = "航空公司")]
        [BsonElement("Airline")]
        public string Airline { get; set; }
        /// <summary>
        /// 航班类型
        /// </summary>
        [Required]
        [BsonElement("FType")]
        [Display(Name = "航班类型")]
        public FlightType FType { get; set; }

        /// <summary>
        /// 桥数据
        /// </summary>
        [Display(Name = "桥数据")]
        [BsonElement("bridgeData")]
        public BridgeModel BridgeData { get; set; }
        /// <summary>
        /// 桥数据
        /// </summary>
        [Display(Name = "空调数据")]
        [BsonElement("pcaData")]
        public PcaModel PcaData { get; set; }
        /// <summary>
        /// 桥数据
        /// </summary>
        [Display(Name = "电源数据")]
        [BsonElement("adapterData")]
        public AdapterModel AdapterData { get; set; }

        /// <summary>
        /// 客梯车数据(次数)
        /// </summary>
        [BsonElement("psData")]
        [Display(Name = "客梯车数据")]
        public int PsData { get; set; }
        /// <summary>
        /// 乘客用车(次数)
        /// </summary>
        [BsonElement("pbData")]
        [Display(Name = "乘客用车")]
        public int PbData { get; set; }
        /// <summary>
        /// 机组用车(次数)
        /// </summary>
        [BsonElement("cbData")]
        [Display(Name = "机组用车")]
        public int CbData { get; set; }
        /// <summary>
        /// 引导用车(次数)
        /// </summary>
        [BsonElement("gData")]
        [Display(Name = "引导用车")]
        public int GData { get; set; }
        /// <summary>
        /// 使用情况
        /// </summary>
        [Display(Name = "使用情况")]
        [BsonElement("Usage")]
        public string Usage { get; set; }
        /// <summary>
        /// 机组签名
        /// </summary>
        [Display(Name = "机组签名")]
        [BsonElement("Pic")]
        public byte[] Pic { get; set; }
        /// <summary>
        /// 工作人员签名
        /// </summary>
        [Display(Name = "工作人员签名")]
        [BsonElement("SPic")]
        public byte[] StaffSignPic { get; set; }
        /// <summary>
        /// 签单时间
        /// </summary>
        [Display(Name = "签单时间")]
        [BsonElement("Time")]
        public DateTime Time { get; set; }

        /// <summary>
        /// 签单状态
        /// </summary>
        [Display(Name = "签单状态")]
        [BsonElement("Status")]
        public SignatureStatus Status { get; set; }
        /// <summary>
        /// 签单创建用户Id
        /// </summary>
        [Display(Name = "签单处理用户")]
        [BsonElement("User")]
        public string UserId { get; set; }

        /// <summary>
        /// 签单创建类型标志
        /// </summary>
        [Display(Name = "签单创建类型标志")]
        [BsonElement("SysBuild")]
        public bool SysBuild { get; set; }
    }
    /// <summary>
    /// 返回成功匹配的签单号模型
    /// </summary>
    [BsonIgnoreExtraElements]
    public class SignatureCheckReturnModel
    {
        /// <summary>
        /// 签单号
        /// </summary>
        [BsonElement("No")]
        public string No { get; set; }
    }
    /// <summary>
    /// Excel模型(普通)
    /// </summary>
    [BsonIgnoreExtraElements]
    public class SignatureExcelModel
    {
        public SignatureExcelModel()
        {
            var flightMergeInstance = MongoDbInstanceStore.DataStoreInstance;
            var builder = Builders<Airline>.Filter;
            var filter = builder.Empty;
            var dBdata = flightMergeInstance.GetDataMany<Airline, Airline>("DataStore", "coll_airline", filter) as IEnumerable<Airline>;
            _airlineData = dBdata;

        }

        private readonly IEnumerable<Airline> _airlineData;
        private string GetAirline(string aCode)
        {
            //var flightMergeInstance = MongoDbInstanceStore.DataStoreInstance;
            //var builder = Builders<Airline>.Filter;
            //var filter = builder.Eq(x => x.Id, aCode);
            //var dBdata = flightMergeInstance.GetDataSingel<Airline, Airline>("DataStore", "coll_airline", filter) as IEnumerable<Airline>;
            foreach (var item in _airlineData)
            {
                if (item.Id==aCode)
                {
                    return item.airline;
                }
            }
            return aCode;
        }
        /// <summary>
        /// 单号
        /// </summary>
        [Display(Name = "签单号")]
        [BsonElement("No")]
        public string No { get; set; }
        /// <summary>
        /// 航班号
        /// </summary>
        [Display(Name = "航班号")]
        [BsonElement("Flt")]
        public string Flt { get; set; }
        /// <summary>
        /// 机尾号
        /// </summary>
        [Display(Name = "机尾号")]
        [BsonElement("Pnum")]
        public string Pnum { get; set; }
        /// <summary>
        /// 机位
        /// </summary>
        [Display(Name = "机位")]
        [BsonElement("Stand")]
        public string Stand { get; set; }
        /// <summary>
        /// 航空公司
        /// </summary>
        //[BsonElement("Airline")]
        [Display(Name = "航空公司")]
        [BsonIgnore]
        private string AirlineSource { get; set; }
        /// <summary>
        /// 航空公司
        /// </summary>
        [Display(Name = "航空公司")]
        [BsonElement("Airline")]
        public string Airline
        {
            get { return GetAirline(AirlineSource); }
            set { AirlineSource = value; }
        }
        /// <summary>
        /// 航班类型
        /// </summary>
        [Display(Name = "航班类型")]
        [BsonElement("FType")]
        [BsonSerializer(typeof(FlightTypeSerializer))]
        public string FType { get; set; }
        /// <summary>
        /// 使用情况
        /// </summary>
        [Display(Name = "使用情况")]
        [BsonElement("Usage")]
        public string Usage { get; set; }
        /// <summary>
        /// 签单时间
        /// </summary>
        [BsonElement("Time")]
        [Display(Name = "签单时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string Time { get; set; }

        /// <summary>
        /// 梯车数量
        /// </summary>
        [BsonElement("psData")]
        [Display(Name = "梯车数量")]
        public int PsData { get; set; }
        /// <summary>
        /// 乘客用车数量
        /// </summary>
        [BsonElement("pbData")]
        [Display(Name = "乘客用车数量")]
        public int PbData { get; set; }
        /// <summary>
        /// 机组用车数量
        /// </summary>
        [BsonElement("cbData")]
        [Display(Name = "机组用车数量")]
        public int CbData { get; set; }
        /// <summary>
        /// 引导车用车数量
        /// </summary>
        [BsonElement("gData")]
        [Display(Name = "机组用车数量")]
        public int GData { get; set; }
        /// <summary>
        /// 桥开始时间
        /// </summary>
        [BsonElement("BridgeBeginTime")]
        [Display(Name = "桥开始时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string BridgeBeginTime { get; set; }
        /// <summary>
        /// 桥结束时间
        /// </summary>
        [BsonElement("BridgeEndTime")]
        [Display(Name = "桥结束时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string BridgeEndTime { get; set; }
        /// <summary>
        /// 桥使用形式
        /// </summary>
        [BsonElement("BridgeUseType")]
        [Display(Name = "桥使用形式")]
        [BsonSerializer(typeof(BridgeUseTypeSerializer))]
        public string BridgeUseType { get; set; }
        /// <summary>
        /// 桥使用
        /// </summary>
        [BsonElement("BridgeUsingTime")]
        [Display(Name = "桥使用时间")]
        public int BridgeUsingTime { get; set; }
        /// <summary>
        /// 空调开始时间
        /// </summary>
        [BsonElement("PcaBeginTime")]
        [Display(Name = "空调开始时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string PcaBeginTime { get; set; }
        /// <summary>
        /// 空调结束时间
        /// </summary>
        [BsonElement("PcaEndTime")]
        [Display(Name = "空调结束时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string PcaEndTime { get; set; }
        /// <summary>
        /// 空调使用形式
        /// </summary>
        [BsonElement("PcaUseType")]
        [Display(Name = "空调使用形式")]
        [BsonSerializer(typeof(BridgeLoadUseTypeSerializer))]
        public string PcaUseType { get; set; }
        /// <summary>
        /// 空调使用时间
        /// </summary>
        [BsonElement("PcaUsingTime")]
        [Display(Name = "空调使用时间")]
        public int PcaUsingTime { get; set; }
        /// <summary>
        /// 电源开始时间
        /// </summary>
        [BsonElement("AdapterBeginTime")]
        [Display(Name = "电源开始时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string AdapterBeginTime { get; set; }
        /// <summary>
        /// 电源结束时间
        /// </summary>
        [BsonElement("AdapterEndTime")]
        [Display(Name = "电源结束时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string AdapterEndTime { get; set; }
        /// <summary>
        /// 电源使用形式
        /// </summary>
        [BsonElement("AdapterUseType")]
        [Display(Name = "电源使用形式")]
        [BsonSerializer(typeof(BridgeLoadUseTypeSerializer))]
        public string AdapterUseType { get; set; }
        /// <summary>
        /// 电源使用时间
        /// </summary>
        [BsonElement("AdapterUsingTime")]
        [Display(Name = "电源使用时间")]
        public int AdapterUsingTime { get; set; }
        ///// <summary>
        ///// 梯车开始时间
        ///// </summary>
        //[BsonElement("PsBeginTime")]
        //[Display(Name = "梯车开始时间")]
        //[BsonSerializer(typeof(PassengerStepBeginTimeDataSerializer))]
        //public string PsBeginTime { get; set; }
        ///// <summary>
        ///// 梯车结束时间
        ///// </summary>
        //[BsonElement("PsEndTime")]
        //[Display(Name = "梯车结束时间")]
        //[BsonSerializer(typeof(PassengerStepEndTimeDataSerializer))]
        //public string PsEndTime { get; set; }
        ///// <summary>
        ///// 梯车使用形式
        ///// </summary>
        //[BsonElement("PsUseType")]
        //[Display(Name = "梯车使用形式")]
        //[BsonSerializer(typeof(PassengerStepUseTypeDataSerializer))]
        //public string PsUseType { get; set; }
        ///// <summary>
        ///// 梯车使用时间
        ///// </summary>
        //[BsonElement("PsUsingTime")]
        //[Display(Name = "梯车使用时间")]
        //[BsonSerializer(typeof(PassengerStepUseTimeDataSerializer))]
        //public string PsUsingTime { get; set; }


        ///// <summary>
        ///// 梯车结束时间
        ///// </summary>
        //[BsonElement("PsEndTime")]
        //[Display(Name = "梯车结束时间")]
        //[BsonSerializer(typeof(DateTimeSerializer))]
        //public object PsEndTime { get; set; }
        ///// <summary>
        ///// 梯车使用形式
        ///// </summary>
        //[BsonElement("PsUseType")]
        //[Display(Name = "梯车使用形式")]
        //public object PsUseType { get; set; }
        ///// <summary>
        ///// 梯车使用时间
        ///// </summary>
        //[BsonElement("PsUsingTime")]
        //[Display(Name = "梯车使用时间")]
        //public object PsUsingTime { get; set; }
    }
    /// <summary>
    /// Excel模型(不普通)
    /// </summary>
    [BsonIgnoreExtraElements]
    public class SignatureExcelExtraModel
    {
        public SignatureExcelExtraModel()
        {
            var flightMergeInstance = MongoDbInstanceStore.DataStoreInstance;
            var builder = Builders<Airline>.Filter;
            var filter = builder.Empty;
            var dBdata = flightMergeInstance.GetDataMany<Airline, Airline>("DataStore", "coll_airline", filter) as IEnumerable<Airline>;
            _airlineData = dBdata;
            _Batch=DateTime.Now.ToString("yyyyMMddHH");

        }

        private readonly IEnumerable<Airline> _airlineData;
        private string AirlineSource { get; set; }

        private string FltSource { get; set; }
        private string FltRTypeSource { get; set; }
        private string BridgeUseTimesSource { get; set; }
        private string _Batch { get; set; }
        private int _BridgeUseTimes { get; set; }

        private string FormatFlt(string sourceData)
        {
            if (sourceData.Contains('/'))
            {
                var fltIn = sourceData.Split('/')[0];
                var fltOut = sourceData.Split('/')[1];
                return fltIn.IsEmpty() ? fltOut : fltIn;
            }
            return sourceData;
        }

        private string FormatFltRType(string sourceData)
        {
            if (sourceData == "国内")
            {
                return "DOM";
            }

            if (sourceData == "国际")
            {
                return "INT";
            }

            return sourceData;
        }

        //private string FormatBridgeUseTimes(string sourceData)
        //{
        //    if (sourceData== BridgeUseType.单.ToString())
        //    {
        //        return "1";
        //    }
        //    if (sourceData == BridgeUseType.双.ToString())
        //    {
        //        return "2";
        //    }
        //    if (sourceData == BridgeUseType.三.ToString())
        //    {
        //        return "3";
        //    }
        //    //switch (sourceData)
        //    //{
        //    //    case BridgeUseType.单:
        //    //        return 1;
        //    //    case (int)BridgeUseType.双:
        //    //        return 2;
        //    //    case (int)BridgeUseType.三:
        //    //        return 3;
        //    //}

        //    return sourceData;
        //}

        private string GetAirline(string aCode)
        {
            foreach (var item in _airlineData)
            {
                if (item.Id == aCode)
                {
                    return item.airline;
                }
            }
            return aCode;
        }
        /// <summary>
        /// 批号
        /// </summary>
        [Display(Name = "批号")]
        public string Batch
        {
            get { return _Batch; }
        }
        /// <summary>
        /// 航班日期
        /// </summary>
        [BsonElement("Fdt")]
        [Display(Name = "航班日期")]
        [BsonSerializer(typeof(DateSerializer))]
        public string Fdt { get; set; }
        /// <summary>
        /// 计划表编号
        /// </summary>
        [Display(Name = "计划表编号")]
        [BsonElement("No")]
        public string No { get; set; }
        /// <summary>
        /// 机尾号
        /// </summary>
        [Display(Name = "机尾号")]
        [BsonElement("Pnum")]
        public string Pnum { get; set; }
        /// <summary>
        /// 航班号
        /// </summary>
        [Display(Name = "航班号")]
        [BsonElement("Flt")]
        public string Flt
        {
            get { return FormatFlt(FltSource);}
            set { FltSource = value; }
        }
        /// <summary>
        /// 航空公司
        /// </summary>
        [Display(Name = "航空公司")]
        [BsonElement("Airline")]
        public string Airline
        {
            get { return GetAirline(AirlineSource); }
            set { AirlineSource = value; }
        }
        /// <summary>
        /// 机型
        /// </summary>
        [Display(Name = "机型")]
        [BsonElement("PlaneType")]
        public string PlaneType { get; set; }
        /// <summary>
        /// 起降标志(到达 出发)
        /// </summary>
        [Display(Name = "起降标志")]
        [BsonElement("TLSign")]
        public string TlSign { get; set; }
        /// <summary>
        /// 航班路线
        /// </summary>
        [Display(Name = "航班路线")]
        [BsonElement("fltr")]
        public string Fltr { get; set; }
        /// <summary>
        /// 航班性质
        /// </summary>
        [Display(Name = "航班性质")]
        [BsonElement("fltRType")]
        public string FltRType
        {
            get { return FormatFltRType(FltRTypeSource);}
            set { FltRTypeSource = value; }
        }
        /// <summary>
        /// 航班任务
        /// </summary>
        [Display(Name = "航班任务")]
        [BsonElement("fltk")]
        public string Fltk { get; set; }
        [BsonElement("Time")]
        [Display(Name = "实际起降时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string Time { get; set; }
        /// <summary>
        /// 空调开始时间
        /// </summary>
        [BsonElement("PcaBeginTime")]
        [Display(Name = "空调开始时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string PcaBeginTime { get; set; }
        /// <summary>
        /// 空调结束时间
        /// </summary>
        [BsonElement("PcaEndTime")]
        [Display(Name = "空调结束时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string PcaEndTime { get; set; }
        /// <summary>
        /// 空调使用时间
        /// </summary>
        [BsonElement("PcaUsingTime")]
        [Display(Name = "空调使用时间")]
        public int PcaUsingTime { get; set; }
        /// <summary>
        /// 电源开始时间
        /// </summary>
        [BsonElement("AdapterBeginTime")]
        [Display(Name = "电源开始时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string AdapterBeginTime { get; set; }
        /// <summary>
        /// 电源结束时间
        /// </summary>
        [BsonElement("AdapterEndTime")]
        [Display(Name = "电源结束时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string AdapterEndTime { get; set; }
        /// <summary>
        /// 电源使用时间
        /// </summary>
        [BsonElement("AdapterUsingTime")]
        [Display(Name = "电源使用时间")]
        public int AdapterUsingTime { get; set; }
        /// <summary>
        /// 梯车开始时间
        /// </summary>
        [BsonElement("PsBeginTime")]
        [Display(Name = "梯车开始时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string PsBeginTime { get; set; }
        /// <summary>
        /// 梯车结束时间
        /// </summary>
        [BsonElement("PsEndTime")]
        [Display(Name = "梯车结束时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string PsEndTime { get; set; }
        /// <summary>
        /// 梯车使用数量
        /// </summary>
        [BsonElement("psData")]
        [Display(Name = "梯车使用次数")]
        public int PsUseTimes { get; set; }
        /// <summary>
        /// 梯车使用时间
        /// </summary>
        [BsonElement("PsUsingTime")]
        [Display(Name = "梯车使用时间")]
        public int PsUsingTime { get; set; }
        /// <summary>
        /// 乘客用车数量
        /// </summary>
        [BsonElement("pbData")]
        [Display(Name = "乘客用车数量")]
        public int PbData { get; set; }
        /// <summary>
        /// 机组用车数量
        /// </summary>
        [BsonElement("cbData")]
        [Display(Name = "机组用车数量")]
        public int CbData { get; set; }
        /// <summary>
        /// 引导车用车数量
        /// </summary>
        [BsonElement("gData")]
        [Display(Name = "机组用车数量")]
        public int GData { get; set; }
        /// <summary>
        /// 桥开始时间
        /// </summary>
        [BsonElement("BridgeBeginTime")]
        [Display(Name = "桥开始时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string BridgeBeginTime { get; set; }
        /// <summary>
        /// 桥结束时间
        /// </summary>
        [BsonElement("BridgeEndTime")]
        [Display(Name = "桥结束时间")]
        [BsonSerializer(typeof(DateTimeSerializer))]
        public string BridgeEndTime { get; set; }
        /// <summary>
        /// 单桥使用时间
        /// </summary>
        [BsonElement("BridgeUsingTime1")]
        [Display(Name = "单桥使用时间")]
        [BsonSerializer(typeof(BridgeUseTimesSerializer))]
        public string BridgeUsingTime1 { get; set; }
        /// <summary>
        /// 双桥使用时间
        /// </summary>
        [BsonElement("BridgeUsingTime2")]
        [BsonSerializer(typeof(BridgeUseTimesSerializer))]
        [Display(Name = "双桥使用时间")]
        public string BridgeUsingTime2 { get; set; }
        /// <summary>
        /// 引导车次
        /// </summary>
        [BsonElement("GuideBusUseTimes")]
        [Display(Name = "引导车次")]
        public int GuideBusUseTimes { get; set; }
        /// <summary>
        /// ACS组织代码
        /// </summary>
        [BsonElement("ACS")]
        [Display(Name = "ACS组织代码")]
        public int ACS
        {
            get { return 5; }
        }

        /// <summary>
        /// 廊桥数量
        /// </summary>
        [BsonElement("BridgeUseType")]
        [Display(Name = "廊桥数量")]
        public int BridgeUseTimes
        {
            get { return _BridgeUseTimes+1; }
            set { _BridgeUseTimes = value; }
        }

        /// <summary>
        /// 航班类型
        /// </summary>
        [Display(Name = "航班类型")]
        [BsonElement("FType")]
        [BsonSerializer(typeof(FlightTypeSerializer))]
        public string FType { get; set; }
    }
    /// <summary>
    /// Excel模型(好不普通)
    /// </summary>
    [BsonIgnoreExtraElements]
    public class SignatureSpecialExcelModel
    {
        [BsonElement("ExcuteTime")]
        [BsonSerializer(typeof(DateSerializer))]
        public string ExcuteTime { get; set; }
        [BsonElement("PlanNo")]
        public string PlanNo { get; set; }
        [BsonElement("Flt")]
        public string Flt { get; set; }
        [BsonElement("Airline")]
        public string Airline { get; set; }
        [BsonElement("Pnum")]
        public string Pnum { get; set; }
        [BsonElement("Pmodel")]
        public string Pmodel { get; set; }
        [BsonElement("BridgeDetail")]
        public  DetailDataModel[] BridgeDetail{ get; set; }
    }
    public class SignatureSpecialExcelFinalModel
    {
        public string ExcuteTime { get; set; }
        public string PlanNo { get; set; }
        public string Flt { get; set; }
        public string Airline { get; set; }
        public string Pnum { get; set; }
        public string Pmodel { get; set; }

        public string BridgeBeginTime { get; set; }
        public string BridgeEndTime { get; set; }

        public decimal? BridgeUsingTime1 { get; set; }
        public decimal? BridgeUsingTime2 { get; set; }
    }
    /// <summary>
    /// 储存桥数据模型
    /// </summary>
    [BsonIgnoreExtraElements]
    public class BridgeModel
    {
        /// <summary>
        /// 桥
        /// </summary>
        //[DataValidation]
        public BridgeData Bridge { get; set; }
        ///// <summary>
        ///// 桥A
        ///// </summary>
        //public BridgeData BridgeA { get; set; }
        ///// <summary>
        ///// 桥B
        ///// </summary>
        //public BridgeData BridgeB { get; set; }
        ///// <summary>
        ///// 桥C
        ///// </summary>
        //public BridgeData BridgeC { get; set; }
    }
    /// <summary>
    /// 单个桥中数据模型
    /// </summary>
    public class BridgeData
    {
        /// <summary>
        /// 桥开始使用时间
        /// </summary>
        public  DateTime? beginTime { get; set; }

        //private DateTime? _beginTime 
        //{
        //    get
        //    {
        //        if (FFIDSource != null)
        //        {
        //            return FFIDSource;
        //        }
        //        else
        //        {
        //            return Id;
        //        }
        //    }
        //    set { beginTime = value; }
        //}

        /// <summary>
        /// 桥结束使用时间
        /// </summary>
        public DateTime? endTime { get; set; }

        /// <summary>
        /// 登机桥使用形式
        /// </summary>
        [EnumCheck(typeof(BridgeUseType))]
        public BridgeUseType UseType { get; set; }

        /// <summary>
        /// 使用时长(分钟)
        /// </summary>
        public int usingTime { get; set; }
    }
    /// <summary>
    /// 储存空调数据模型
    /// </summary>
    [BsonIgnoreExtraElements]
    public class PcaModel
    {
        /// <summary>
        /// 空调
        /// </summary>
        //[DataValidation]
        public PcaData Pca { get; set; }
        ///// <summary>
        ///// 空调1
        ///// </summary>
        //public PcaData PcaA { get; set; }
        ///// <summary>
        ///// 空调2
        ///// </summary>
        //public PcaData PcaB { get; set; }


    }
    /// <summary>
    /// 单部空调的数据模型
    /// </summary>
    public class PcaData
    {
        /// <summary>
        /// 空调开始使用时间
        /// </summary>
        public DateTime? beginTime { get; set; }
        /// <summary>
        /// 空调结束使用时间
        /// </summary>
        public DateTime? endTime { get; set; }

        /// <summary>
        /// 空调使用形式
        /// </summary>
        [EnumCheck(typeof(BridgeLoadUseType))]
        public BridgeLoadUseType UseType { get; set; }
        /// <summary>
        /// 使用时长(分钟)
        /// </summary>
        public int usingTime { get; set; }
    }
    /// <summary>
    /// 储存电源数据模型
    /// </summary>
    [BsonIgnoreExtraElements]
    public class AdapterModel
    {
        /// <summary>
        /// 电源
        /// </summary>
        //[DataValidation]
        public AdapterData Adapter { get; set; }
        ///// <summary>
        ///// 电源A1
        ///// </summary>
        //public AdapterData AdapterA1 { get; set; }
        ///// <summary>
        ///// 电源A2
        ///// </summary>
        //public AdapterData AdapterA2 { get; set; }
        ///// <summary>
        ///// 电源B1
        ///// </summary>
        //public AdapterData AdapterB1 { get; set; }
        ///// <summary>
        ///// 电源B2
        ///// </summary>
        //public AdapterData AdapterB2 { get; set; }
    }
    /// <summary>
    /// 储存电源数据模型
    /// </summary>
    public class AdapterData
    {
        /// <summary>
        /// 电源开始使用时间
        /// </summary>
        public DateTime? beginTime { get; set; }
        /// <summary>
        /// 电源结束使用时间
        /// </summary>
        public DateTime? endTime { get; set; }

        /// <summary>
        /// 电源使用形式
        /// </summary>
        [EnumCheck(typeof(BridgeLoadUseType))]
        [Required]
        public BridgeLoadUseType UseType { get; set; }
        /// <summary>
        /// 使用时长(分钟)
        /// </summary>
        public int usingTime { get; set; }
    }

    public class DetailDataModel
    {
        [BsonElement("_id")]
        public DetailDataEquMsg Id { get; set; }
        [BsonElement("DetailData")]
        public List<EquDetailData> DetailDatas { get; set; }
    }

    public class DetailDataEquMsg
    {
        public int mid { get; set; }
        public string n { get; set; }
    }

    public class EquDetailData
    {
        public DateTime? beginTime { get; set; }
        public DateTime? endTime { get; set; }
        [BsonRepresentation(BsonType.Double, AllowTruncation = true)]
        public decimal? usingTime { get; set; }
    }

    ///// <summary>
    ///// 存储客梯车数据模型
    ///// </summary>
    //public class PassengerStepModel
    //{
    //    /// <summary>
    //    /// 一个储存客梯车使用的数组类
    //    /// </summary>
    //    public PassengerStepData[] PassengerStep { get; set; }
    //}
    /// <summary>
    /// 客梯车数据模型
    /// </summary>
    public class PassengerStepData
    {
        /// <summary>
        /// 梯车开始使用时间
        /// </summary>
        
        public DateTime? beginTime { get; set; }
        /// <summary>
        /// 梯车结束使用时间
        /// </summary>
        public DateTime? endTime { get; set; }
        /// <summary>
        /// 客梯车使用形式
        /// </summary>
        [EnumCheck(typeof(BridgeUseType))]
        public BridgeUseType UseType { get; set; }
        /// <summary>
        /// 使用时长(分钟)
        /// </summary>
        public int usingTime { get; set; }
    }
    /// <summary>
    /// 储存单据计数
    /// </summary>
    public class ListCountStore
    {
        /// <summary>
        /// 当前的DataTime
        /// </summary>
        public DateTime? Date { get; set; }
        /// <summary>
        /// 当日签单号计数
        /// </summary>
        public int Count { get; set; }
    }
    /// <summary>
    /// 表头模型
    /// </summary>
    public class TableHeadModel
    {
        /// <summary>
        /// 表格名称
        /// </summary>
        public string Table { get; set; }

        /// <summary>
        /// 标题对象key val对
        /// </summary>
        public object Titles { get; set; }
    }
    /// <summary>
    /// 签单状态
    /// </summary>
    public enum SignatureStatus
    {

        /// <summary>
        /// 已创建
        /// </summary>
        已创建,
        /// <summary>
        /// 已签名
        /// </summary>
        已签名,
        /// <summary>
        /// 已完结
        /// </summary>
        已完结,
        /// <summary>
        /// 已删除
        /// </summary>
        已删除
    }
    /// <summary>
    /// 签单更新模式
    /// </summary>
    public enum SignatureUpdateType
    {
        /// <summary>
        /// 自动更新
        /// </summary>
        Auto,
        /// <summary>
        /// 手工在线更新
        /// </summary>
        ManaulOnline,
        /// <summary>
        /// 手工离线更新
        /// </summary>
        ManaulOffline
    }
    /// <summary>
    /// 设备使用情况
    /// </summary>
    public enum SignatureUsage
    {
        正常使用,
        故障未使用,
        机组未用,
        使用中故障停用
    }
    public enum SignatureType
    {
        /// <summary>
        /// 正常签单
        /// </summary>
        普通,
        /// <summary>
        /// 重靠桥
        /// </summary>
        二次靠桥,
        /// <summary>
        /// 其他
        /// </summary>
        其它
    }
    /// <summary>
    /// 桥和客梯车使用形式
    /// </summary>
    public enum BridgeUseType
    {
        /// <summary>
        /// 单桥
        /// </summary>
        单,
        /// <summary>
        /// 双桥
        /// </summary>
        双,
        /// <summary>
        /// 三桥
        /// </summary>
        三
    }
    /// <summary>
    /// 桥载使用形式
    /// </summary>
    public enum BridgeLoadUseType
    {
        /// <summary>
        /// 窄体机
        /// </summary>
        窄,
        /// <summary>
        /// 宽体机
        /// </summary>
        宽,
        /// <summary>
        /// A380(空调*3电源*3)
        /// </summary>
        A380,
        /// <summary>
        /// A380((空调*3电源*4))
        /// </summary>
        A380_2
    }
    /// <summary>
    /// 导出模式
    /// </summary>
    public enum ExportMode
    {
        /// <summary>
        /// 按时间范围模式
        /// </summary>
        range,
        /// <summary>
        /// 按月月模式
        /// </summary>
        month
    }
    /// <summary>
    /// 航空公司
    /// </summary>
    [BsonIgnoreExtraElements]
    public class Airline
    {
        /// <summary>
        /// 航空公司代号
        /// </summary>
        [BsonElement("_id")]
        public string  Id { get; set; }
        /// <summary>
        /// 航空公司
        /// </summary>
        [BsonElement("CNNM")]
        public string airline { get; set; }
    }
    /// <summary>
    /// 请求获取WebSocketTokenModel
    /// </summary>
    public class WebSocketRequsetTokenModel
    {
        [Required]
        [EnumCheck(typeof(WebSocketAction))]
        public WebSocketAction WsAct { get; set; }
    }
    /// <summary>
    /// WebSocketToken返回模型
    /// </summary>
    [BsonIgnoreExtraElements]
    public class WebSocketTokenModel
    {
        /// <summary>
        /// 用户id
        /// </summary>
        [BsonElement("user")]
        public string User { get; set; }
        /// <summary>
        /// 连接WebSocket的Token
        /// </summary>
        [BsonElement("WsToken")]
        public string Token { get; set; }


    }
    /// <summary>
    /// WebSocket请求连接模型
    /// </summary>
    public class WebSocketRequestModel
    {
        /// <summary>
        /// 用户id
        /// </summary>
        [Required]
        public string User { get; set; }
        /// <summary>
        /// 连接WebSocket的Token
        /// </summary>
        [Required]
        public string Token { get; set; }

        /// <summary>
        /// 此次连接对应的操作
        /// </summary>
        [Required]
        [EnumCheck(typeof(WebSocketAction))]
        public WebSocketAction WsAct { get; set; }
    }
    public enum WebSocketAction
    {
        /// <summary>
        /// Excel进度条
        /// </summary>
        ExcelProgeress,
        /// <summary>
        /// 推送通用信息
        /// </summary>
        PushMessage,
        /// <summary>
        /// 东一指廊
        /// </summary>
        East1,
        /// <summary>
        /// 东二指廊
        /// </summary>
        East2,
        /// <summary>
        /// 东三指廊
        /// </summary>
        East3,
        /// <summary>
        /// 东五指廊
        /// </summary>
        East5,
        /// <summary>
        /// 东六指廊
        /// </summary>
        East6,
        /// <summary>
        /// 东连接楼
        /// </summary>
        EastConnect,
        /// <summary>
        /// 西一指廊
        /// </summary>
        West1,
        /// <summary>
        /// 西二指廊
        /// </summary>
        West2,
        /// <summary>
        /// 西三指廊
        /// </summary>
        West3,
        /// <summary>
        /// 西五指廊
        /// </summary>
        West5,
        /// <summary>
        /// 西六指廊
        /// </summary>
        West6,
        /// <summary>
        /// 西连接楼
        /// </summary>
        WestConnect,
        /// <summary>
        /// 北指廊
        /// </summary>
        North,
        /// <summary>
        /// T1远机位
        /// </summary>
        RemotePositionT1,
        /// <summary>
        /// 东T2远机位
        /// </summary>
        EastRemotePositionT2,
        /// <summary>
        /// 西T2远机位
        /// </summary>
        WestRemotePositionT2
    }
    public class WebSocketStore
    {
       public  string User { get; set; }
       public string Token { get; set; }
       public WebSocket WebSocketInstance { get; set; }
    }
}

namespace SignatureWebApi.Formatters
{
    /// <summary>
    /// 签单列表格式化为Excel格式的接口实现
    /// </summary>
    public class SignatureListExcelFormatter : BufferedMediaTypeFormatter
    {
        private const string MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";//"application/vnd.ms-excel"
        /// <summary>
        /// 签单列表格式化为Excel格式的接口实现(构造函数)
        /// </summary>
        public SignatureListExcelFormatter()
        {
            // Add the supported media type.
            SupportedMediaTypes.Add(new MediaTypeHeaderValue(MIME_TYPE));

        }
        /// <summary>
        /// 实现可反序列化接口 直接返回false
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public override bool CanReadType(Type type)
        {
            return false;
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 将数据写入流的实现
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <param name="writeStream"></param>
        /// <param name="content"></param>
        public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
        {
            var clientList = value as IEnumerable<SignatureExcelModel>;
            if (clientList != null)
            {
                SignatureExcelModel[] signatureExcelModels;
                try
                {
                    signatureExcelModels = clientList as SignatureExcelModel[] ?? clientList.ToArray();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                XSSFWorkbook exbook;
                try
                {
                    var mongoDb = MongoDbInstanceStore.DataStoreInstance;
                    var builder = Builders<BsonDocument>.Filter;
                    var filter = builder.Eq("Table", "SignatureExcel");
                    var projection = Builders<BsonDocument>.Projection.Exclude("_id");
                    var dBdata = mongoDb.GetDataSingel<BsonDocument, BsonDocument>("DataStore", "TitleStore", filter, projection)as IEnumerable<BsonDocument>;
                    var TitleData = dBdata.ToList()[0].ToBsonDocument().ToList()[1].Value.ToBsonDocument().ToList();
                    List<string> Title=new List<string>();
                    for (int i = 0; i < TitleData.Count; i++)
                    {
                        Title.Add(TitleData[i].Value.ToString());
                    }

                    var token=HttpContext.Current.Request.Params.Get("token");
                    exbook = OtherHelper.DataConvertToExcel(signatureExcelModels, WebSocketHelper.SendExportProgress,"单月签单表",Title, token);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                exbook.Write(writeStream);
                exbook.Clear();
                exbook.Close();
                //writeStream.Flush();
                //writeStream.Position = 0;
                writeStream.Close();
                writeStream.Dispose();
                GC.Collect();
            }
            else
            {
                var singleObj = value as SignatureExcelModel;
                if (singleObj == null)
                {
                    throw new InvalidOperationException("Cannot serialize type");
                }
            }

            //const string filename = "clientStatusLog";
            ////content.Headers.ContentType = new MediaTypeHeaderValue(MIME_TYPE);
            //content.Headers.Add("Content-Disposition", $"attachment;filename={filename + ".xlsx"}");
            ////content.Headers.ContentDisposition = new ContentDispositionHeaderValue( "attachment;filename=test.xlsx");
            ////content.Headers.ContentDisposition.FileName = filename;
        }

        /// <summary>
        /// 实现可序列化接口
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public override bool CanWriteType(Type type)
        {
            if (type == typeof(SignatureExcelModel))
            {
                return true;
            }

            Type enumerableType = typeof(IEnumerable<SignatureExcelModel>);
            return enumerableType.IsAssignableFrom(type);
            //throw new NotImplementedException();
        }
    }

    public class SignatureListExcelExtraFormatter : BufferedMediaTypeFormatter
    {
        private const string MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";//"application/vnd.ms-excel"
        /// <summary>
        /// 签单列表格式化为Excel格式的接口实现(构造函数)
        /// </summary>
        public SignatureListExcelExtraFormatter()
        {
            // Add the supported media type.
            SupportedMediaTypes.Add(new MediaTypeHeaderValue(MIME_TYPE));

        }
        /// <summary>
        /// 实现可反序列化接口 直接返回false
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public override bool CanReadType(Type type)
        {
            return false;
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 将数据写入流的实现
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <param name="writeStream"></param>
        /// <param name="content"></param>
        public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
        {
            var clientList = value as IEnumerable<SignatureExcelExtraModel>;
            var writer = new StreamWriter(writeStream);
            if (clientList != null)
            {
                SignatureExcelExtraModel[] signatureExcelModels;
                try
                {
                    signatureExcelModels = clientList as SignatureExcelExtraModel[] ?? clientList.ToArray();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                XSSFWorkbook exbook;
                try
                {
                    var mongoDb = MongoDbInstanceStore.DataStoreInstance;
                    var builder = Builders<BsonDocument>.Filter;
                    var filter = builder.Eq("Table", "SignatureExcelExtra");
                    var projection = Builders<BsonDocument>.Projection.Exclude("_id");
                    var dBdata = mongoDb.GetDataSingel<BsonDocument, BsonDocument>("DataStore", "TitleStore", filter, projection) as IEnumerable<BsonDocument>;
                    var TitleData = dBdata.ToList()[0].ToBsonDocument().ToList()[1].Value.ToBsonDocument().ToList();
                    List<string> Title = new List<string>();
                    for (int i = 0; i < TitleData.Count; i++)
                    {
                        Title.Add(TitleData[i].Value.ToString());
                    }

                    var token = HttpContext.Current.Request.Params.Get("token");
                    exbook = OtherHelper.DataConvertToExcel(signatureExcelModels,WebSocketHelper.SendExportProgress, "单月签单表", Title, token);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                exbook.Write(writeStream);
                exbook.Clear();
                exbook.Close();
                //writeStream.Flush();
                //writeStream.Position = 0;
                writeStream.Close();
                writeStream.Dispose();
                GC.Collect();
                //writeStream.Flush();
                //exbook.Dispose();
            }
            else
            {
                var singleObj = value as SignatureExcelExtraModel;
                if (singleObj == null)
                {
                    throw new InvalidOperationException("Cannot serialize type");
                }
            }

            //const string filename = "clientStatusLog";
            ////content.Headers.ContentType = new MediaTypeHeaderValue(MIME_TYPE);
            //content.Headers.Add("Content-Disposition", $"attachment;filename={filename + ".xlsx"}");
            ////content.Headers.ContentDisposition = new ContentDispositionHeaderValue( "attachment;filename=test.xlsx");
            ////content.Headers.ContentDisposition.FileName = filename;
        }

        /// <summary>
        /// 实现可序列化接口
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public override bool CanWriteType(Type type)
        {
            if (type == typeof(SignatureExcelExtraModel))
            {
                return true;
            }

            Type enumerableType = typeof(IEnumerable<SignatureExcelExtraModel>);
            return enumerableType.IsAssignableFrom(type);
            //throw new NotImplementedException();
        }
    }

    public class SignatureListExcelSpecialFormatter : BufferedMediaTypeFormatter
    {
        private const string MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";//"application/vnd.ms-excel"
        /// <summary>
        /// 签单列表格式化为Excel格式的接口实现(构造函数)
        /// </summary>
        public SignatureListExcelSpecialFormatter()
        {
            // Add the supported media type.
            SupportedMediaTypes.Add(new MediaTypeHeaderValue(MIME_TYPE));

        }
        /// <summary>
        /// 实现可反序列化接口 直接返回false
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public override bool CanReadType(Type type)
        {
            return false;
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 将数据写入流的实现
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <param name="writeStream"></param>
        /// <param name="content"></param>
        public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
        {
            var clientList = value as IEnumerable<SignatureSpecialExcelFinalModel>;
            if (clientList != null)
            {
                SignatureSpecialExcelFinalModel[] signatureExcelModels;
                try
                {
                    signatureExcelModels = clientList as SignatureSpecialExcelFinalModel[] ?? clientList.ToArray();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                XSSFWorkbook exbook;
                try
                {
                    var mongoDb = MongoDbInstanceStore.DataStoreInstance;
                    var builder = Builders<BsonDocument>.Filter;
                    var filter = builder.Eq("Table", "SignatureSpecialExcelModel");
                    var projection = Builders<BsonDocument>.Projection.Exclude("_id");
                    var dBdata = mongoDb.GetDataSingel<BsonDocument, BsonDocument>("DataStore", "TitleStore", filter, projection) as IEnumerable<BsonDocument>;
                    var TitleData = dBdata.ToList()[0].ToBsonDocument().ToList()[1].Value.ToBsonDocument().ToList();
                    List<string> Title = new List<string>();
                    for (int i = 0; i < TitleData.Count; i++)
                    {
                        Title.Add(TitleData[i].Value.ToString());
                    }

                    var token = HttpContext.Current.Request.Params.Get("token");
                    exbook = OtherHelper.DataConvertToExcel(signatureExcelModels, WebSocketHelper.SendExportProgress, "签单表", Title, token);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                exbook.Write(writeStream);
                exbook.Clear();
                exbook.Close();
                //writeStream.Flush();
                //writeStream.Position = 0;
                writeStream.Close();
                writeStream.Dispose();
                GC.Collect();
            }
            else
            {
                var singleObj = value as SignatureSpecialExcelFinalModel;
                if (singleObj == null)
                {
                    throw new InvalidOperationException("Cannot serialize type");
                }
            }
        }

        /// <summary>
        /// 实现可序列化接口
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public override bool CanWriteType(Type type)
        {
            if (type == typeof(SignatureSpecialExcelFinalModel))
            {
                return true;
            }

            Type enumerableType = typeof(IEnumerable<SignatureSpecialExcelFinalModel>);
            return enumerableType.IsAssignableFrom(type);
            //throw new NotImplementedException();
        }
    }
}

namespace SignatureWebApi
{
    /// <summary>
    /// 
    /// </summary>
    public class FlightTypeSerializer : IBsonSerializer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            
            try
            {
                var bsonReader = context.Reader.ReadInt32();
                foreach (var status in Enum.GetValues(typeof(FlightType)))
                {
                    if ((int)status == bsonReader)
                    {
                        return status.ToString();
                    }
                }
                return bsonReader.ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return " ";
                //Console.WriteLine(E);
                //throw;
            }
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <param name="value"></param>
        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {
            var bsonWriter = context.Writer;
           
            bsonWriter.WriteString(value.ToString());
            
        }

        /// <summary>
        /// 
        /// </summary>
        public Type ValueType => typeof(string);
    }
    /// <summary>
    /// 桥使用模式
    /// </summary>
    public class BridgeUseTypeSerializer : IBsonSerializer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
           
            try
            {
                var bsonReader = context.Reader.ReadInt32();
                foreach (var status in Enum.GetValues(typeof(BridgeUseType)))
                {
                    if ((int)status == bsonReader)
                    {
                        return status.ToString();
                    }
                }
                return bsonReader.ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return " ";
                //throw;
            }
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <param name="value"></param>
        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {
            var bsonWriter = context.Writer;

            var Temp = " ";
            if (value != null)
            {
                Temp = value.ToString();
            }
            bsonWriter.WriteString(Temp);

        }

        /// <summary>
        /// 
        /// </summary>
        public Type ValueType => typeof(string);
    }
    /// <summary>
    /// 桥载模式
    /// </summary>
    public class BridgeLoadUseTypeSerializer : IBsonSerializer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {

            try
            {
                var bsonReader = context.Reader.ReadInt32();
                foreach (var status in Enum.GetValues(typeof(BridgeLoadUseType)))
                {
                    if ((int)status == bsonReader)
                    {
                        return status.ToString();
                    }
                }
                return bsonReader.ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return " ";
                //Console.WriteLine(E);
                //throw;
            }
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <param name="value"></param>
        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {
            var bsonWriter = context.Writer;

            var Temp = " ";
            if (value != null)
            {
                Temp = value.ToString();
            }
            bsonWriter.WriteString(Temp);

        }

        /// <summary>
        /// 
        /// </summary>
        public Type ValueType => typeof(string);
    }
    /// <summary>
    /// 桥载模式
    /// </summary>
    public class BridgeUseTimesSerializer : IBsonSerializer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {

            try
            {
                if (context.Reader.State == BsonReaderState.Value)
                {

                    switch (context.Reader.CurrentBsonType)
                    {
                        case BsonType.Double:
                        {
                            var bsonReader = context.Reader.ReadDouble();
                                var result = Math.Round(bsonReader / 60, 2);
                                if (result > 0)
                                {
                                    return result.ToString(CultureInfo.InvariantCulture);
                                }

                            return " ";
                        }
                        case BsonType.Int32:
                        {
                                var bsonReader = context.Reader.ReadInt32();
                                var result = Math.Round((double)bsonReader / 60, 2);
                                if (result > 0)
                                {
                                    return result.ToString(CultureInfo.InvariantCulture);
                                }

                                return " ";
                        }
                        case BsonType.Null:
                        {
                            context.Reader.ReadNull();
                            return "";
                        }

                    }
                }
                return " ";
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return " ";
                //Console.WriteLine(E);
                //throw;
            }
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <param name="value"></param>
        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {
            var bsonWriter = context.Writer;

            var Temp = " ";
            if (value != null)
            {
                Temp = value.ToString();
            }
            bsonWriter.WriteString(Temp);

        }

        /// <summary>
        /// 
        /// </summary>
        public Type ValueType => typeof(string);
    }
    /// <summary>
    /// mongoDB时间格式化方法类
    /// </summary>
    public class DateTimeSerializer : IBsonSerializer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            long bsonReader;
            try
            {
                if (context.Reader.State == BsonReaderState.Value)
                {

                    if (context.Reader.CurrentBsonType!=BsonType.Null)
                    {
                        bsonReader = context.Reader.ReadDateTime();
                        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
                        DateTime dt = startTime.AddMilliseconds(bsonReader);
                        return dt.ToString("yyyy-MM-dd HH:mm:00");
                    }
                    context.Reader.ReadNull();
                }

                //var qqqqq = context.Reader.ReadBsonType();
                //var BsonType = MongoDB.Bson.BsonType.DateTime;
                //if (BsonType!=BsonType.Null)
                //{
                //    bsonReader = context.Reader.ReadDateTime();
                //    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                //    DateTime dt = startTime.AddMilliseconds(bsonReader);
                //    return dt.ToString("yyyy/MM/dd HH:mm:ss");
                //}
                return " ";
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return " ";

            }
             //context.Reader.ReadDateTime();

            
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <param name="value"></param>
        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {
            var bsonWriter = context.Writer;
            var temp = " ";
            if (value!=null)
            {
                temp = value.ToString();
            }
            bsonWriter.WriteString(temp);

        }

        /// <summary>
        /// 
        /// </summary>
        public Type ValueType => typeof(string);
    }
    /// <summary>
    /// mongoDb日期格式化方法类
    /// </summary>
    public class DateSerializer : IBsonSerializer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            long bsonReader;
            try
            {
                if (context.Reader.State == BsonReaderState.Value)
                {

                    if (context.Reader.CurrentBsonType != BsonType.Null)
                    {
                        bsonReader = context.Reader.ReadDateTime();
                        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
                        DateTime dt = startTime.AddMilliseconds(bsonReader);
                        return dt.ToString("yyyyMMdd");
                    }
                    context.Reader.ReadNull();
                }

                //var qqqqq = context.Reader.ReadBsonType();
                //var BsonType = MongoDB.Bson.BsonType.DateTime;
                //if (BsonType!=BsonType.Null)
                //{
                //    bsonReader = context.Reader.ReadDateTime();
                //    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                //    DateTime dt = startTime.AddMilliseconds(bsonReader);
                //    return dt.ToString("yyyy/MM/dd HH:mm:ss");
                //}
                return " ";
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return " ";

            }
            //context.Reader.ReadDateTime();


            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="args"></param>
        /// <param name="value"></param>
        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {
            var bsonWriter = context.Writer;
            var temp = " ";
            if (value != null)
            {
                temp = value.ToString();
            }
            bsonWriter.WriteString(temp);

        }

        /// <summary>
        /// 
        /// </summary>
        public Type ValueType => typeof(string);
    }
    /// <summary>
    /// 客梯车开始时间序列化
    /// </summary>
    public class PassengerStepBeginTimeDataSerializer : IBsonSerializer
    {
        /// <inheritdoc />
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {

            PassengerStepData targetObj = new PassengerStepData();
            try
            {
                
                context.Reader.ReadStartDocument();
                DateTime? zeroTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                while (context.Reader.State!= BsonReaderState.EndOfDocument)
                {
                    // ReSharper disable once InconsistentNaming
                    var BsonType = context.Reader.ReadBsonType();
                    if (BsonType==BsonType.EndOfDocument)
                    {
                        break;
                    }
                    var Field = context.Reader.ReadName();
                    switch (Field)
                    {
                        case "beginTime":
                        {
                            if (BsonType != BsonType.Null)
                            {
                                targetObj.beginTime = zeroTime.Value.AddMilliseconds(context.Reader.ReadDateTime());
                            }
                            else
                            {
                                context.Reader.ReadNull();
                                targetObj.beginTime = null;

                            }
                        };
                            break;
                        case "endTime":
                        {
                                if (BsonType !=BsonType.Null)
                                {
                                    targetObj.endTime = zeroTime.Value.AddMilliseconds(context.Reader.ReadDateTime());
                                }
                                else
                                {
                                    context.Reader.ReadNull();
                                    targetObj.endTime = null;

                                }
                        }
                            break;
                        case "UseType":
                        {
                            if (BsonType!=BsonType.Null)
                            {
                                    try
                                    {
                                        var useType = (BridgeUseType)System.Enum.Parse(typeof(BridgeUseType), context.Reader.ReadInt32().ToString());
                                        targetObj.UseType = useType;
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine(e);
                                        targetObj.UseType = BridgeUseType.单;
                                    }
                            }
                            else
                            {
                                targetObj.UseType = BridgeUseType.单;
                                context.Reader.ReadNull();
                            }

                        }

                            break;
                        case "usingTime":
                        {
                            if (BsonType!=BsonType.Null)
                            {
                                targetObj.usingTime = context.Reader.ReadInt32();
                            }
                            else
                            {
                                context.Reader.ReadNull();
                                targetObj.usingTime = 0;
                            }
                        }
                            break;
                    }
                }

                {
                    //DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                    //DateTime st = startTime.AddMilliseconds(context.Reader.ReadDateTime());

                    //TargetObj.beginTime = st;
                    //System.DateTime endTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区

                    ////var tttttt = context.Reader.ReadBsonType();
                    //DateTime et = endTime.AddMilliseconds(context.Reader.ReadDateTime());
                    //TargetObj.endTime = et;

                    //var useType = (BridgeUseType)System.Enum.Parse(typeof(BridgeUseType), context.Reader.ReadInt32().ToString());
                    //TargetObj.UseType = useType;

                    //TargetObj.usingTime = context.Reader.ReadInt32();
                    //context.Reader.ReadEndDocument();
                }
                context.Reader.ReadEndDocument();
                if (targetObj.beginTime==null)
                {
                    return "";
                }

                return targetObj.beginTime.GetValueOrDefault().ToString("yyyy/MM/dd HH:mm:ss");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        /// <inheritdoc />
        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {

            var bsonWriter = context.Writer;
            var Temp = " ";
            if (value != null)
            {
                Temp = value.ToString();
            }
            bsonWriter.WriteString(Temp);
        }

        /// <inheritdoc />
        public Type ValueType => typeof(string);
    }
    /// <summary>
    /// 客梯车结束时间序列化
    /// </summary>
    public class PassengerStepEndTimeDataSerializer : IBsonSerializer
    {
        /// <inheritdoc />
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {

            PassengerStepData TargetObj = new PassengerStepData();
            try
            {

                context.Reader.ReadStartDocument();
                DateTime? zeroTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                while (context.Reader.State != BsonReaderState.EndOfDocument)
                {
                    var BsonType = context.Reader.ReadBsonType();
                    if (BsonType == BsonType.EndOfDocument)
                    {
                        break;
                    }
                    var Field = context.Reader.ReadName();
                    switch (Field)
                    {
                        case "beginTime":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    TargetObj.beginTime = zeroTime.Value.AddMilliseconds(context.Reader.ReadDateTime());
                                }
                                else
                                {
                                    context.Reader.ReadNull();
                                    TargetObj.beginTime = null;

                                }
                            };
                            break;
                        case "endTime":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    TargetObj.endTime = zeroTime.Value.AddMilliseconds(context.Reader.ReadDateTime());
                                }
                                else
                                {
                                    context.Reader.ReadNull();
                                    TargetObj.endTime = null;

                                }
                            }
                            break;
                        case "UseType":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    try
                                    {
                                        var useType = (BridgeUseType)System.Enum.Parse(typeof(BridgeUseType), context.Reader.ReadInt32().ToString());
                                        TargetObj.UseType = useType;
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine(e);
                                        TargetObj.UseType = BridgeUseType.单;
                                    }
                                }
                                else
                                {
                                    TargetObj.UseType = BridgeUseType.单;
                                    context.Reader.ReadNull();
                                }

                            }

                            break;
                        case "usingTime":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    TargetObj.usingTime = context.Reader.ReadInt32();
                                }
                                else
                                {
                                    context.Reader.ReadNull();
                                    TargetObj.usingTime = 0;
                                }
                            }
                            break;
                    }
                }

                {
                    //DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                    //DateTime st = startTime.AddMilliseconds(context.Reader.ReadDateTime());

                    //TargetObj.beginTime = st;
                    //System.DateTime endTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区

                    ////var tttttt = context.Reader.ReadBsonType();
                    //DateTime et = endTime.AddMilliseconds(context.Reader.ReadDateTime());
                    //TargetObj.endTime = et;

                    //var useType = (BridgeUseType)System.Enum.Parse(typeof(BridgeUseType), context.Reader.ReadInt32().ToString());
                    //TargetObj.UseType = useType;

                    //TargetObj.usingTime = context.Reader.ReadInt32();
                    //context.Reader.ReadEndDocument();
                }
                context.Reader.ReadEndDocument();
                if (TargetObj.endTime == null)
                {
                    return "";
                }

                return TargetObj.endTime.GetValueOrDefault().ToString("yyyy/MM/dd HH:mm:ss");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        /// <inheritdoc />
        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {

            var bsonWriter = context.Writer;
            var Temp = " ";
            if (value != null)
            {
                Temp = value.ToString();
            }
            bsonWriter.WriteString(Temp);
        }

        /// <inheritdoc />
        public Type ValueType => typeof(string);
    }
    /// <summary>
    /// 客梯车使用形式序列化
    /// </summary>
    public class PassengerStepUseTypeDataSerializer : IBsonSerializer
    {
        /// <inheritdoc />
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {

            PassengerStepData TargetObj = new PassengerStepData();
            try
            {

                context.Reader.ReadStartDocument();
                DateTime? zeroTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                while (context.Reader.State != BsonReaderState.EndOfDocument)
                {
                    var BsonType = context.Reader.ReadBsonType();
                    if (BsonType == BsonType.EndOfDocument)
                    {
                        break;
                    }
                    var Field = context.Reader.ReadName();
                    switch (Field)
                    {
                        case "beginTime":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    TargetObj.beginTime = zeroTime.Value.AddMilliseconds(context.Reader.ReadDateTime());
                                }
                                else
                                {
                                    context.Reader.ReadNull();
                                    TargetObj.beginTime = null;

                                }
                            };
                            break;
                        case "endTime":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    TargetObj.endTime = zeroTime.Value.AddMilliseconds(context.Reader.ReadDateTime());
                                }
                                else
                                {
                                    context.Reader.ReadNull();
                                    TargetObj.endTime = null;

                                }
                            }
                            break;
                        case "UseType":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    try
                                    {
                                        var useType = (BridgeUseType)System.Enum.Parse(typeof(BridgeUseType), context.Reader.ReadInt32().ToString());
                                        TargetObj.UseType = useType;
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine(e);
                                        TargetObj.UseType = BridgeUseType.单;
                                    }
                                }
                                else
                                {
                                    TargetObj.UseType = BridgeUseType.单;
                                    context.Reader.ReadNull();
                                }

                            }

                            break;
                        case "usingTime":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    TargetObj.usingTime = context.Reader.ReadInt32();
                                }
                                else
                                {
                                    context.Reader.ReadNull();
                                    TargetObj.usingTime = 0;
                                }
                            }
                            break;
                    }
                }

                {
                    //DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                    //DateTime st = startTime.AddMilliseconds(context.Reader.ReadDateTime());

                    //TargetObj.beginTime = st;
                    //System.DateTime endTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区

                    ////var tttttt = context.Reader.ReadBsonType();
                    //DateTime et = endTime.AddMilliseconds(context.Reader.ReadDateTime());
                    //TargetObj.endTime = et;

                    //var useType = (BridgeUseType)System.Enum.Parse(typeof(BridgeUseType), context.Reader.ReadInt32().ToString());
                    //TargetObj.UseType = useType;

                    //TargetObj.usingTime = context.Reader.ReadInt32();
                    //context.Reader.ReadEndDocument();
                }
                context.Reader.ReadEndDocument();
                if (TargetObj.beginTime == null)
                {
                    return "";
                }

                return TargetObj.UseType.ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        /// <inheritdoc />
        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {

            var bsonWriter = context.Writer;
            var Temp = " ";
            if (value != null)
            {
                Temp = value.ToString();
            }
            bsonWriter.WriteString(Temp);
        }

        /// <inheritdoc />
        public Type ValueType => typeof(string);
    }
    /// <summary>
    /// 客梯车使用时间序列化
    /// </summary>
    public class PassengerStepUseTimeDataSerializer : IBsonSerializer
    {
        /// <inheritdoc />
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {

            PassengerStepData TargetObj = new PassengerStepData();
            try
            {

                context.Reader.ReadStartDocument();
                DateTime? zeroTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                while (context.Reader.State != BsonReaderState.EndOfDocument)
                {
                    var BsonType = context.Reader.ReadBsonType();
                    if (BsonType == BsonType.EndOfDocument)
                    {
                        break;
                    }
                    var Field = context.Reader.ReadName();
                    switch (Field)
                    {
                        case "beginTime":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    TargetObj.beginTime = zeroTime.Value.AddMilliseconds(context.Reader.ReadDateTime());
                                }
                                else
                                {
                                    context.Reader.ReadNull();
                                    TargetObj.beginTime = null;

                                }
                            };
                            break;
                        case "endTime":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    TargetObj.endTime = zeroTime.Value.AddMilliseconds(context.Reader.ReadDateTime());
                                }
                                else
                                {
                                    context.Reader.ReadNull();
                                    TargetObj.endTime = null;

                                }
                            }
                            break;
                        case "UseType":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    try
                                    {
                                        var useType = (BridgeUseType)System.Enum.Parse(typeof(BridgeUseType), context.Reader.ReadInt32().ToString());
                                        TargetObj.UseType = useType;
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine(e);
                                        TargetObj.UseType = BridgeUseType.单;
                                    }
                                }
                                else
                                {
                                    TargetObj.UseType = BridgeUseType.单;
                                    context.Reader.ReadNull();
                                }

                            }

                            break;
                        case "usingTime":
                            {
                                if (BsonType != BsonType.Null)
                                {
                                    TargetObj.usingTime = context.Reader.ReadInt32();
                                }
                                else
                                {
                                    context.Reader.ReadNull();
                                    TargetObj.usingTime = 0;
                                }
                            }
                            break;
                    }
                }

                {
                    //DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                    //DateTime st = startTime.AddMilliseconds(context.Reader.ReadDateTime());

                    //TargetObj.beginTime = st;
                    //System.DateTime endTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区

                    ////var tttttt = context.Reader.ReadBsonType();
                    //DateTime et = endTime.AddMilliseconds(context.Reader.ReadDateTime());
                    //TargetObj.endTime = et;

                    //var useType = (BridgeUseType)System.Enum.Parse(typeof(BridgeUseType), context.Reader.ReadInt32().ToString());
                    //TargetObj.UseType = useType;

                    //TargetObj.usingTime = context.Reader.ReadInt32();
                    //context.Reader.ReadEndDocument();
                }
                context.Reader.ReadEndDocument();
                if (TargetObj.beginTime == null)
                {
                    return "";
                }

                return TargetObj.UseType.ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        /// <inheritdoc />
        public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
        {

            var bsonWriter = context.Writer;
            var Temp = " ";
            if (value != null)
            {
                Temp = value.ToString();
            }
            bsonWriter.WriteString(Temp);
        }

        /// <inheritdoc />
        public Type ValueType => typeof(string);
    }

    /// <summary>
    /// 结束时间验证
    /// </summary>
    public class DataValidation : ValidationAttribute
    {
        /// <summary>
        /// 设置是否忽略结束时间
        /// </summary>
        /// <param name="ignoreEndTime">true为忽略结束时间,false为检验结束时间</param>
        public DataValidation(bool ignoreEndTime=false)
        {
            _ignoreEndTime = ignoreEndTime;
        }

        private string _errMsg="";

        private bool _ignoreEndTime=false;
        /// <summary>
        /// 创建一个委托来处理不同类型的验证
        /// </summary>
        private delegate bool ValidDelegate();
        /// <summary>
        /// 错误输出数据
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public override string FormatErrorMessage(string name)
        {
            return _errMsg;
        }
        /// <summary>
        /// 验证结束时间有效
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool IsValid(object value)
        {
            if (value==null)
            {
                return true;
            }
            //现在只验证时间
            var @switch = new Dictionary<Type, ValidDelegate> {
                { typeof(BridgeModel), () => ValidBridgeTime(value)},
                { typeof(PcaModel), () => ValidPcaTime(value)},
                { typeof(AdapterModel), () => ValidAdapterTime(value)}
            };
            return @switch[value.GetType()]();
            {
                //var valTemp =value as BridgeData;
                //if (valTemp==null)
                //{
                //    return true;
                //}
                //if (valTemp.beginTime == null)
                //{
                //    var endTime = valTemp.endTime;
                //    if (endTime == null)
                //    {
                //        return true;
                //    }
                //    _errMsg = "没有填写开始时间!";
                //    return false;
                //    //throw new ValidationException("没有填写开始时间!");

                //}
                //else
                //{
                //    var endTime = valTemp.endTime;
                //    if (endTime==null)
                //    {
                //        _errMsg = "没有填写结束时间!";
                //        return false;
                //    }
                //    var countTime=OtherHelper.CountUsingTime(valTemp.beginTime.GetValueOrDefault(), endTime.GetValueOrDefault());
                //    if (countTime >= 0) return true;
                //    _errMsg = "结束时间早于开始时间,请检查输入!";
                //    return false;
                //}
            }
            // return false;
        }

        private bool ValidBridgeTime(object value)
        {
            var valModel = value as BridgeModel;
            if (valModel==null)
            {
                return true;
            }
            var valTemp = valModel.Bridge;

            if (valTemp == null)
            {
                return true;
            }
            if (valTemp.beginTime == null)
            {
                var endTime = valTemp.endTime;
                if (endTime == null)
                {
                    return true;
                }
                _errMsg = "没有填写开始时间!";
                return false;
                //throw new ValidationException("没有填写开始时间!");

            }
            else
            {
                var endTime = valTemp.endTime;
                if (endTime == null)
                {
                    _errMsg = "没有填写结束时间!";

                    return _ignoreEndTime;
                }
                var countTime = OtherHelper.CountUsingTime(valTemp.beginTime.GetValueOrDefault(), endTime.GetValueOrDefault());
                if (countTime >= 0) return true;
                _errMsg = "结束时间早于开始时间,请检查输入!";
                return false;
            }
        }
        private bool ValidPcaTime(object value)
        {
            var valModel = value as PcaModel;
            if (valModel == null)
            {
                return true;
            }
            var valTemp = valModel.Pca;
            if (valTemp == null)
            {
                return true;
            }
            if (valTemp.beginTime == null)
            {
                var endTime = valTemp.endTime;
                if (endTime == null)
                {
                    return true;
                }
                _errMsg = "没有填写开始时间!";
                return false;
                //throw new ValidationException("没有填写开始时间!");

            }
            else
            {
                var endTime = valTemp.endTime;
                if (endTime == null)
                {
                    _errMsg = "没有填写结束时间!";
                    return _ignoreEndTime;
                }
                var countTime = OtherHelper.CountUsingTime(valTemp.beginTime.GetValueOrDefault(), endTime.GetValueOrDefault());
                if (countTime >= 0) return true;
                _errMsg = "结束时间早于开始时间,请检查输入!";
                return false;
            }
        }
        private bool ValidAdapterTime(object value)
        {
            var valModel = value as AdapterModel;
            if (valModel == null)
            {
                return true;
            }
            var valTemp = valModel.Adapter;
            if (valTemp == null)
            {
                return true;
            }
            if (valTemp.beginTime == null)
            {
                var endTime = valTemp.endTime;
                if (endTime == null)
                {
                    return true;
                }
                _errMsg = "没有填写开始时间!";
                return false;
                //throw new ValidationException("没有填写开始时间!");

            }
            else
            {
                var endTime = valTemp.endTime;
                if (endTime == null)
                {
                    _errMsg = "没有填写结束时间!";
                    return _ignoreEndTime;
                }
                var countTime = OtherHelper.CountUsingTime(valTemp.beginTime.GetValueOrDefault(), endTime.GetValueOrDefault());
                if (countTime >= 0) return true;
                _errMsg = "结束时间早于开始时间,请检查输入!";
                return false;
            }
        }
    }
}
